package Eclass;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import login.UserModel;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import validationInput.ValidateInput;

import Candidate.CandidateForm;
import Eclass.Batch.BatchManager;
import Eclass.BatchClass.*;
import Eclass.Score.ScoreForm;
import Forum.ForumThreadForm;
import Master.SchoolForm;
import Quartz.QuartzModel;
import Quartz.QuartzPlugin;
public class EclassAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EclassForm eclass = (EclassForm) form;
		EclassManager eclassManager = new EclassManager();
		BatchManager batchManager = new BatchManager();
		ValidateInput vi = new ValidateInput();
		HttpSession session = request.getSession(true);
		System.out.println("*********Task : "+ eclass.getTask());
		boolean result;
		
		//-------------------------------------------------------------- Navigation ----------------------------------------------------------
				CandidateForm candidateForm = new CandidateForm();
				SchoolForm schoolForm = new SchoolForm();
				//EclassForm eClassForm = new EclassForm();
				ForumThreadForm forumForm = new ForumThreadForm();
				ScoreForm scoreForm = new ScoreForm();
				
				if("updateCandidate".equalsIgnoreCase(eclass.getTask())) {
//					System.out.println("lahhh");
//					candidateForm.getcModel().setRegisNo("01-13-12- 060");
//					candidateForm.setTask("update");
//					return mapping.findForward("candidate");
				}else if("manageTrainee".equalsIgnoreCase(eclass.getTask())) {	
					candidateForm.setTask("manageTrainee");
					return mapping.findForward("candidate");
//				}else if("batch".equalsIgnoreCase(eclass.getTask())) {
//					eClassForm.setTask("batch");
//					return mapping.findForward("eclass");
//				}else if("listExamPrep".equalsIgnoreCase(eclass.getTask())) {
//					eClassForm.setTask("listExamPrep");
//					return mapping.findForward("eclass");
				}else if("manageSchool".equalsIgnoreCase(eclass.getTask())) {
					schoolForm.setTask("manageSchool");
					return mapping.findForward("master");
		  		}else if("forumPage".equalsIgnoreCase(eclass.getTask())) {
					forumForm.setTask("forumPage");
					return mapping.findForward("forum");
				}else if("report".equalsIgnoreCase(eclass.getTask())) {
					scoreForm.setTask("report");
					return mapping.findForward("score");
				}else if("manageJob".equalsIgnoreCase(eclass.getTask())){
					schoolForm.setTask("manageJob");
					return mapping.findForward("master");
				}
		//------------------------------------------------------------------------------------------------------------------------------
	
		//manage user
		if("selectUser".equalsIgnoreCase(eclass.getTask())){			
			session.setAttribute("selectUserId", eclass.getModelEclass().getUserid());
			System.out.println("select User test : " + session.getAttribute("selectUserId"));
			EclassModel model = (EclassModel)eclassManager.selectAll(eclass.getModelEclass().getUserid()).get(0);			
			eclass.setModelEclass(model);
			System.out.println("Model : " + eclass.getModelEclass().getUserid());
			//eclass.setListUser(eclassManager.selectAll(null));
			return null;
		}
				
		//--------------------------------------------------------------
			
		if ("accountSetting".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("accountSetting");
		}
		if ("manageUser".equalsIgnoreCase(eclass.getTask())) {
			
		}
		if ("update".equalsIgnoreCase(eclass.getTask())) {
			eclass.setModelEclass((EclassModel)eclassManager.selectAll(eclass.getUpdateUser()).get(0));
			return mapping.findForward("updateUser");
		}
		if ("resetPassword".equalsIgnoreCase(eclass.getTask())){
			eclass.getModelEclass().setUsrupd(((UserModel)session.getAttribute("userLogin")).getUserid());
			eclassManager.resetPassword(eclass.getModelEclass());
			
			eclass.setModelEclass((EclassModel)eclassManager.selectAll(eclass.getModelEclass().getUserid()).get(0));
			return mapping.findForward("updateUser");
		}
		if ("updateUser".equalsIgnoreCase(eclass.getTask())){
			eclass.getModelEclass().setUsrupd(((UserModel)session.getAttribute("userLogin")).getUserid());
			eclassManager.updateUser(eclass.getModelEclass());
			
			eclass.setListUser(eclassManager.selectAll(null));
			return mapping.findForward("manageUser");
		}
//		if ("manageTrainee".equalsIgnoreCase(eclass.getTask())) {
//			return mapping.findForward("manageTrainee");
//		}
		if ("manageCandidate".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("manageCandidate");
		}
		if ("batch".equalsIgnoreCase(eclass.getTask())) {
			eclass.setListBatch(batchManager.selectAll());
			return mapping.findForward("manageBatch");
		}
		if ("updateBatch".equalsIgnoreCase(eclass.getTask())) {
			eclass.setModelBatch(batchManager.selectBatch(eclass.getBatchId()));
			return mapping.findForward("updateBatch");
		}
		if ("updateBatchExe".equalsIgnoreCase(eclass.getTask())) {
			System.out.println(eclass.getModelBatch());
			boolean res = batchManager.updateBatch(eclass.getModelBatch());
			if(res){
				session.setAttribute("result", "Batch update successfully");
			}else{
				session.setAttribute("result", "Batch update failed");
			}
			eclass.setListBatch(batchManager.selectAll());
			return mapping.findForward("manageBatch");
		}
		if ("insertBatch".equalsIgnoreCase(eclass.getTask())) {
			boolean res = batchManager.addBatch(eclass.getModelBatch());
			if(res){
				session.setAttribute("result", "Batch insert successfully");
			}else{
				session.setAttribute("result", "Batch insert failed");
			}
			eclass.setListBatch(batchManager.selectAll());
			return mapping.findForward("manageBatch");
		}
		if ("subject".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("manageSubject");
		}
		if ("updateSubject".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("updateSubject");
		}
		if ("insertSubject".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("insertSubject");
		}
		if ("deleteSubject".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("manageSubject");
		}
		if ("schedule".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("listSchedule");
		}
		if ("insertSchedule".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("insertSchedule");
		}
		if ("updateSchedule".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("updateSchedule");
		}
		if ("deleteSchedule".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("listSchedule");
		}
		if ("fileSharing".equalsIgnoreCase(eclass.getTask())) {
			return mapping.findForward("fileSharing");
		}
		if ("score".equalsIgnoreCase(eclass.getTask())){
			
			return mapping.findForward("score");
		}

		
		//START EXAM PREPARATION
		if ("listExamPrep".equalsIgnoreCase(eclass.getTask())) {
			eclass = setListExamPrep(eclass);
			return mapping.findForward("listExamPrep");
		}
		
		//Rere 31 Des 2013
		if("viewExamPrep".equalsIgnoreCase(eclass.getTask())){			
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
			eclass.setModelExamPrep(exPrepMgr.getExamPrepByExamPrepId(examPrepId));
			eclass.setListBatchClass(new BatchClassManager().getListUniqueExamBatchClassWithAddition(eclass.getModelExamPrep().getBatchClassId()));
			
			if("trainee".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
				List list = new ArrayList();
				list = exPrepMgr.getExamPrepDetByExamPrepId(examPrepId);
				if(list.size() > 0){
					eclass.setListExamPrepDetail(list);
					eclass.setListClass(exPrepMgr.getListClassFrmQuestPool(eclass.getModelExamPrep().getExamFor()));
					
					for(int i=0; i<list.size(); i++){
						//set total no of question
						((ExamPrepDetailModel)list.get(i)).setTotalNoOfQuestion(exPrepMgr.getListClassFrmQuestPoolById(((ExamPrepDetailModel)list.get(i)).getClassId()).getNoOfQuestion());
					}				
				}
			}else{
				eclass.getModelExamPrep().setTotNumOfQuestCandidate(exPrepMgr.getNumOfQuestForCandidate());
			}
			
			return mapping.findForward("viewExamPrep");
		}
		//=============================
		
		if ("backToListExamPrep".equalsIgnoreCase(eclass.getTask())) {
			
			//	BELUM ADA QUARTZ
			System.out.println("backToListExamPrep");
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepModel epm = eclass.getModelExamPrep();
			System.out.println("---------------- backToListExamPrep : "+epm);
			List examPrepDet = exPrepMgr.getExamPrepDetByExamPrepId(epm.getExamPrepId());
			
			//Quartz--------
			QuartzPlugin qPlugin = new QuartzPlugin();
			QuartzModel quartzModel = new QuartzModel();

			quartzModel.setExamPrepId(epm.getExamPrepId().toString());
			//---
			
			if(examPrepDet == null || (examPrepDet!=null && examPrepDet.size() < 1)){
				epm.setStatus("0");
				if(exPrepMgr.updateStatusExamPrep(epm)){
					qPlugin.DeleteAction(quartzModel);
					System.out.println("Success Update Status Exam Prep!!!!!!!!");
				}
				else System.out.println("Failed to Update Status Exam Prep!!!!!!!!");
			}
			eclass = setListExamPrep(eclass);
			return mapping.findForward("listExamPrep");
		}
		
		if ("addExamPrep".equalsIgnoreCase(eclass.getTask())) {
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			eclass.setListBatchClass(new BatchClassManager().getListUniqueExamBatchClass());
			ExamPrepModel examPrepModel = new ExamPrepModel();
			Integer examPrepId = exPrepMgr.getCurExamPrepId();
			examPrepModel.setExamPrepId(examPrepId);
			examPrepModel.validateModel();
			eclass.setModelExamPrep(examPrepModel);
			eclass.setTitle("Add New Exam Preparation");
			eclass.setTask("insertExamPrep");	
			
			//Rere, 27 Des 2013 : set blank detail exam prep
			eclass.setListClass(exPrepMgr.getListClassFrmQuestPool("Trainee"));
			List list = new ArrayList();
			ExamPrepDetailModel detailModel = new ExamPrepDetailModel();
			detailModel.setClassId(((ClassModel)eclass.getListClass().get(0)).getClassId());
			detailModel.setNoOfQuestion(0);
			detailModel.setTotalNoOfQuestion(0);
			list.add(detailModel);
			eclass.setListExamPrepDetail(list);					
			
			return mapping.findForward("addEditExamPrep");
		}
		
		if ("editExamPrep".equalsIgnoreCase(eclass.getTask())) {
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
			eclass.setModelExamPrep(exPrepMgr.getExamPrepByExamPrepId(examPrepId));
			eclass.setListBatchClass(new BatchClassManager().getListUniqueExamBatchClassWithAddition(eclass.getModelExamPrep().getBatchClassId()));
			eclass.setTitle("Update Exam Preparation");
			eclass.setTask("updateExamPrep");
			
			//Rere, 27 Des 2013
			List list = new ArrayList();
			list = exPrepMgr.getExamPrepDetByExamPrepId(examPrepId);	
			System.out.println("Isi list : " + list.size());
			if(list.size() > 0){
				eclass.setListExamPrepDetail(list);
				eclass.setListClass(exPrepMgr.getListClassFrmQuestPool(eclass.getModelExamPrep().getExamFor()));
			}else{
				eclass.setListClass(exPrepMgr.getListClassFrmQuestPool("Trainee"));				
				ExamPrepDetailModel detailModel = new ExamPrepDetailModel();
				detailModel.setClassId(((ClassModel)eclass.getListClass().get(0)).getClassId());
				detailModel.setNoOfQuestion(0);
				detailModel.setTotalNoOfQuestion(0);
				list.add(detailModel);
				System.out.println("List : " + list.size());
				eclass.setListExamPrepDetail(list);
			}		
			
			return mapping.findForward("addEditExamPrep");
		}
		
		if ("insertExamPrep".equalsIgnoreCase(eclass.getTask())) {
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			eclass = setNullExamPrep(eclass);

//			 -------------Willian java quartz--------
			String examOpen, examClosed;
			examOpen = eclass.getModelExamPrep().getStartHour().replaceAll("/", ":");
			examOpen = examOpen.replaceAll(" ", ":");
			examClosed = eclass.getModelExamPrep().getEndHour().replaceAll("/", ":");
			examClosed = examClosed.replaceAll(" ", ":");

			QuartzPlugin qPlugin = new QuartzPlugin();
			QuartzModel quartzModel = new QuartzModel();

			String[] open = examOpen.split(":");
			String[] closed = examClosed.split(":");
			quartzModel.setExamPrepId(eclass.getModelExamPrep().getExamPrepId().toString());
			quartzModel.setDateOpen(open[0]);
			quartzModel.setMonthOpen(open[1]);
			quartzModel.setYearOpen(open[2]);
			
			if(eclass.getModelExamPrep().getExamFor().equals("Trainee")){
				quartzModel.setHourOpen(open[3]);
				quartzModel.setMinuteOpen(open[4]);
			}else{
				quartzModel.setHourOpen("23");
				quartzModel.setMinuteOpen("59");
			}
			
			quartzModel.setDateClosed(closed[0]);
			quartzModel.setMonthClosed(closed[1]);
			quartzModel.setYearClosed(closed[2]);
			quartzModel.setHourClosed(closed[3]);
			quartzModel.setMinuteClosed(closed[4]);
						
			quartzModel.setExamFor(eclass.getModelExamPrep().getExamFor());
			//quartzModel.setStatus(eclass.getModelExamPrep().getStatus());

			System.out.println("-------->" + examOpen + "      " + examClosed);
			System.out.println(eclass.getModelExamPrep().getEndHour());

			// ----------------------------------------------------------
			
//			Boolean cek = false;
//			if(cekStartEndDate(eclass.getModelExamPrep().getStartHour(), eclass.getModelExamPrep().getEndHour(), eclass.getModelExamPrep().getExamFor())){			
//				if(exPrepMgr.insertExamPrep(eclass.getModelExamPrep())) {
//					cek = true;
//					if(insertExamPrepDetailForCandidate(eclass,exPrepMgr)){
//						session.setAttribute("result", "Success Insert New Exam Preparation Detail");
//					}
//					else{
//						session.setAttribute("result", "Failed to Insert New Exam Preparation Detail");
//					}
//				}
//			}			

//			if(cek){
//				if (eclass.getModelExamPrep().getStatus().equals("1") && eclass.getModelExamPrep().getExamFor().equalsIgnoreCase("Trainee")) {				
//					qPlugin.Action(quartzModel);
//				}
//				session.setAttribute("result", "Success Insert New Exam Schedule Preparation");
//			}else{
//				session.setAttribute("result", "Failed to Insert New Exam Schedule Preparation");
//				eclass = setListExamPrep(eclass);
//				return mapping.findForward("listExamPrep");
//			}
			
//			setAfterInsUpdtExamPrep(eclass,exPrepMgr);
//			if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
//				return mapping.findForward("listExamPrep");
//			}
//			return mapping.findForward("addEditExamPrepDetail");
			
		//Rere 31 Des 2013 ===============
			Boolean cek = false;
			if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
				//cek duration untuk candidate
				cek = cekDuration(eclass.getModelExamPrep().getDuration(), eclass.getModelExamPrep().getExamFor());
				System.out.println("Cek Duration : " + cek);
			}else{
				//cek start end date untuk trainee
				cek = cekStartEndDate(eclass.getModelExamPrep().getStartHour(), eclass.getModelExamPrep().getEndHour(), eclass.getModelExamPrep().getExamFor());
				System.out.println("Cek Start End Date : " + cek);
			}
			
			if(cek){	
				//Insert master
				System.out.println("\nBatch Class ID : " + eclass.getModelExamPrep().getBatchClassId());
				System.out.println("Duration : " + eclass.getModelExamPrep().getDuration());
				System.out.println("Start Hour : " + eclass.getModelExamPrep().getStartHour());
				System.out.println("End Hour : " + eclass.getModelExamPrep().getEndHour());
				System.out.println("Status : " + eclass.getModelExamPrep().getStatus());
				System.out.println("Exam For : " + eclass.getModelExamPrep().getExamFor());
				System.out.println("Is Open : " + eclass.getModelExamPrep().getIsOpen());
				System.out.println();
				result = exPrepMgr.insertExamPrep(eclass.getModelExamPrep());
				
				if(result){
					//Insert detail
					//Integer examPrepId = exPrepMgr.getCurExamPrepId(); //get current examPrepID
					Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
					if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
						result = insertExamPrepDetailForCandidate(eclass,exPrepMgr);
					}else{ //trainee detail
						System.out.println("Class ID Length : " + eclass.getClassId().length);
						
						for(int i=0; i<eclass.getClassId().length; i++){
							ExamPrepDetailModel detailModel = new ExamPrepDetailModel();
							detailModel.setExamPrepId(examPrepId);
							detailModel.setClassId(eclass.getClassId()[i]);
							detailModel.setNoOfQuestion(eclass.getNoOfQuestion()[i]);
							
							System.out.println("Exam Prep ID : " + examPrepId);
							System.out.println("Class ID : " + eclass.getClassId()[i]);
							System.out.println("No Of Question : " + eclass.getNoOfQuestion()[i]);
							
							result = exPrepMgr.insertExamPrepDetail(detailModel);
						}
					}
				}				
			}
			
			if(cek){
				if (eclass.getModelExamPrep().getStatus().equals("1") && eclass.getModelExamPrep().getExamFor().equalsIgnoreCase("Trainee")) {				
					qPlugin.Action(quartzModel);
				}
				session.setAttribute("result", "Success Insert New Exam Schedule Preparation");
			}else{
				session.setAttribute("result", "Failed to Insert New Exam Schedule Preparation");
			}
			
			eclass = setListExamPrep(eclass);
			return mapping.findForward("listExamPrep");
		//=======================================
			
		}
		
		if ("updateExamPrep".equalsIgnoreCase(eclass.getTask())) {
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			eclass = setNullExamPrep(eclass);
			ExamPrepModel modelExamPrep = eclass.getModelExamPrep();
			
		// Willian-Quartz----------------------------------------------
			String examOpen, examClosed;
			examOpen = modelExamPrep.getStartHour().replaceAll("/", ":");
			examOpen = examOpen.replaceAll(" ", ":");
			examClosed = modelExamPrep.getEndHour().replaceAll("/", ":");
			examClosed = examClosed.replaceAll(" ", ":");

			QuartzPlugin qPlugin = new QuartzPlugin();
			QuartzModel quartzModel = new QuartzModel();

			String[] open = examOpen.split(":");
			String[] closed = examClosed.split(":");
			quartzModel.setExamPrepId(modelExamPrep.getExamPrepId().toString());
			quartzModel.setDateOpen(open[0]);
			quartzModel.setMonthOpen(open[1]);
			quartzModel.setYearOpen(open[2]);
			
			if(eclass.getModelExamPrep().getExamFor().equals("Trainee")){
				quartzModel.setHourOpen(open[3]);
				quartzModel.setMinuteOpen(open[4]);
			}else{
				quartzModel.setHourOpen("23");
				quartzModel.setMinuteOpen("59");
			}

			quartzModel.setDateClosed(closed[0]);
			quartzModel.setMonthClosed(closed[1]);
			quartzModel.setYearClosed(closed[2]);
			quartzModel.setHourClosed(closed[3]);
			quartzModel.setMinuteClosed(closed[4]);	
			
			quartzModel.setExamFor(eclass.getModelExamPrep().getExamFor());
			System.out.println("---------->STATUS"+eclass.getModelExamPrep().getStatus());
			quartzModel.setStatus(eclass.getModelExamPrep().getStatus());

		// ---------------------------------------------
			
			//System.out.println(modelExamPrep);
//			Boolean cek = false;
//			if(cekStartEndDate(eclass.getModelExamPrep().getStartHour(), eclass.getModelExamPrep().getEndHour(), eclass.getModelExamPrep().getExamFor())){			
//				if(exPrepMgr.updateExamPrep(modelExamPrep)){
//					cek = true;
//					if(insertExamPrepDetailForCandidate(eclass, exPrepMgr)){
//						session.setAttribute("result", "Success Insert/Update New Exam Preparation Detail");
//					}
//					else{
//						session.setAttribute("result", "Failed to Insert/Update New Exam Preparation Detail");
//					}
//				}
//			}
//			
//			if(cek){
//				qPlugin.UpdateAction(quartzModel);
//				if(eclass.getModelExamPrep().getIsOpen().equals("1")){
//					exPrepMgr.cancelExamPrep(eclass.getModelExamPrep().getExamPrepId());
//					exPrepMgr.cancelExamScore(eclass.getModelExamPrep().getBatchClassId());
//				}
//				session.setAttribute("result", "Success Update New Exam Schedule Preparation");
//			}else{
//				session.setAttribute("result", "Failed to Update New Exam Schedule Preparation");
//				eclass = setListExamPrep(eclass);
//				return mapping.findForward("listExamPrep");
//			}
//			
//			setAfterInsUpdtExamPrep(eclass,exPrepMgr);
//			if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
//				return mapping.findForward("listExamPrep");
//			}
//			return mapping.findForward("addEditExamPrepDetail");
			
		//Rere 31 Des 2013 ===============
			Boolean cek = false;
			if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
				//cek duration untuk candidate
				cek = cekDuration(eclass.getModelExamPrep().getDuration(), eclass.getModelExamPrep().getExamFor());
				System.out.println("Cek Duration : " + cek);
			}else{
				//cek start end date untuk trainee
				cek = cekStartEndDate(eclass.getModelExamPrep().getStartHour(), eclass.getModelExamPrep().getEndHour(), eclass.getModelExamPrep().getExamFor());
				System.out.println("Cek Start End Date : " + cek);
			}
			
			if(cek){	
				//Update master
				System.out.println("\nBatch Class ID : " + eclass.getModelExamPrep().getBatchClassId());
				System.out.println("Duration : " + eclass.getModelExamPrep().getDuration());
				System.out.println("Start Hour : " + eclass.getModelExamPrep().getStartHour());
				System.out.println("End Hour : " + eclass.getModelExamPrep().getEndHour());
				System.out.println("Status : " + eclass.getModelExamPrep().getStatus());
				System.out.println("Exam For : " + eclass.getModelExamPrep().getExamFor());
				System.out.println("Is Open : " + eclass.getModelExamPrep().getIsOpen());
				System.out.println();
				result = exPrepMgr.updateExamPrep(eclass.getModelExamPrep());
				
				if(result){
					//Insert detail
					Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();//get current examPrepID
					result = exPrepMgr.deleteExamPrepDetByExamPrepId(examPrepId); //delete all detail 
					
					if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
						Integer exPrepId = eclass.getModelExamPrep().getExamPrepId();
						if( exPrepId!=null && exPrepMgr.getNumOfExPrepDetCandByExamPrepId(exPrepId)>0 ){
							ExamPrepDetailModel expdm = new ExamPrepDetailModel();
							expdm.setExamPrepId(eclass.getModelExamPrep().getExamPrepId());
							expdm.setNoOfQuestion(eclass.getModelExamPrep().getNumOfQuestCandidate());
							result = exPrepMgr.updateExPrepDetNumOfQuestForCandidate(expdm);
						}
						else{
							result = insertExamPrepDetailForCandidate(eclass, exPrepMgr);
						}
					}else{ //trainee detail
						System.out.println("Class ID Length : " + eclass.getClassId().length);
						
						for(int i=0; i<eclass.getClassId().length; i++){
							ExamPrepDetailModel detailModel = new ExamPrepDetailModel();
							detailModel.setExamPrepId(examPrepId);
							detailModel.setClassId(eclass.getClassId()[i]);
							detailModel.setNoOfQuestion(eclass.getNoOfQuestion()[i]);
							
							System.out.println("Exam Prep ID : " + examPrepId);
							System.out.println("Class ID : " + eclass.getClassId()[i]);
							System.out.println("No Of Question : " + eclass.getNoOfQuestion()[i]);
							
							result = exPrepMgr.insertExamPrepDetail(detailModel);
						}
					}
				}				
			}
			
			if(cek){
				qPlugin.UpdateAction(quartzModel);
				if(eclass.getModelExamPrep().getIsOpen().equals("1")){
					exPrepMgr.cancelExamPrep(eclass.getModelExamPrep().getExamPrepId());
					exPrepMgr.cancelExamScore(eclass.getModelExamPrep().getBatchClassId());
				}
				session.setAttribute("result", "Success Update New Exam Schedule Preparation");
			}else{
				session.setAttribute("result", "Failed to Update New Exam Schedule Preparation");				
			}
			
			eclass = setListExamPrep(eclass);
			return mapping.findForward("listExamPrep");
		//=======================================
			
		}
		
		if ("deleteExamPrep".equalsIgnoreCase(eclass.getTask())) {
			//willian----------
			QuartzPlugin qPlugin = new QuartzPlugin();
			QuartzModel quartzModel = new QuartzModel();

			quartzModel.setExamPrepId(eclass.getModelExamPrep().getExamPrepId().toString());
			
			//------------------
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer idExamPrep = eclass.getModelExamPrep().getExamPrepId();
			if(exPrepMgr.deleteExamPrepDetByExamPrepId(idExamPrep)){
				if(exPrepMgr.deleteExamPrep(idExamPrep)){
					if(eclass.getModelExamPrep().getExamFor().equals("Trainee")){
					qPlugin.DeleteAction(quartzModel);
					System.out.println("DELETE SCHEDULER TRAINEE");
					}
					session.setAttribute("result", "Success Delete Exam Preparation");
				}
				else session.setAttribute("result", "Failed to Delete Exam Preparation");
			}
			else eclass.setResultQuery("Failed to Delete Exam Preparation Detail");
			
			eclass = setListExamPrep(eclass);
			return mapping.findForward("listExamPrep");
		}
		
		if ("insertExamPrepDetail".equalsIgnoreCase(eclass.getTask())) {
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
			ExamPrepDetailModel expdm = eclass.getModelExamPrepDetail();
			expdm.setExamPrepId(examPrepId);
			expdm.validateModel();
			if(exPrepMgr.insertExamPrepDetail(expdm)){
				session.setAttribute("result", "Success Insert New Exam Preparation Detail");
			}
			else{
				session.setAttribute("result", "Failed to Insert New Exam Preparation Detail");
			}
			
			eclass = configListExamPrepDetail(eclass,exPrepMgr,examPrepId);
			return mapping.findForward("addEditExamPrepDetail");
		}
		
		if(("updateExamPrepDetail".equalsIgnoreCase(eclass.getTask()))){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
			ExamPrepDetailModel expdm = eclass.getModelExamPrepDetail();
			expdm.setExamPrepId(examPrepId);
			expdm.validateModel();
			
			if(exPrepMgr.updateExamPrepDetail(expdm)){
				session.setAttribute("result", "Success Update Exam Preparation Detail - "+examPrepId);
			}
			else{
				session.setAttribute("result", "Failed to Update Exam Preparation Detail - " + examPrepId);
			}
			eclass = configListExamPrepDetail(eclass,exPrepMgr,examPrepId);
			return mapping.findForward("addEditExamPrepDetail");
		}	
				
		if("editSubjectExamPrepDet".equalsIgnoreCase(eclass.getTask())){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepDetailModel epdm = eclass.getModelExamPrepDetail();
			Integer idExamPrepDetail = epdm.getExamPrepDetailId();
			ExamPrepDetailModel epdm2 = exPrepMgr.getExamPrepDetByExamPrepDetId(idExamPrepDetail);
			ExamPrepModel epm = new ExamPrepModel();
			epm.setExamPrepId(epdm.getExamPrepId());
			epm.validateModel();
			epdm2.validateModel();
			eclass.setModelExamPrepDetail(epdm2);
			eclass.setListClass(getUniqueClassIdWithAddition(eclass,exPrepMgr,epdm2.getClassId()));
			return mapping.findForward("editSubjectExamPrepDet");
		}
		
		if("updateSubjectDetail".equalsIgnoreCase(eclass.getTask())){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepDetailModel epdm = eclass.getModelExamPrepDetail();
			epdm.validateModel();
			if(exPrepMgr.updateExamPrepDetailByIdExmPrepDet(epdm)){
				session.setAttribute("result", "Success Update Subject Exam Preparation Detail");
			}
			else session.setAttribute("result", "Failed to Update Subject Exam Preparation Detail");
			
			eclass = configListExamPrepDetail(eclass,exPrepMgr,epdm.getExamPrepId());
			return mapping.findForward("addEditExamPrepDetail");
		}
		
		if("deleteSubjectExamPrepDet".equalsIgnoreCase(eclass.getTask())){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepDetailModel epdm = eclass.getModelExamPrepDetail();
			epdm.validateModel();
			Integer idExamPrepDetail = epdm.getExamPrepDetailId();
			if(exPrepMgr.deleteExamPrepDetByExamPrepDetId(idExamPrepDetail)){
				session.setAttribute("result", "Success Delete Subject Exam Preparation Detail");
			}
			else session.setAttribute("result", "Failed to Delete Subject Exam Preparation Detail");
			
			System.out.println("epdm.getExamPrepId() : "+epdm.getExamPrepId());
			eclass.setListExamPrepDetail(exPrepMgr.getExamPrepDetByExamPrepId(eclass.getModelExamPrep().getExamPrepId()));
			eclass.setListClass(getUniqueClassId(eclass, exPrepMgr));
			return mapping.findForward("addEditExamPrepDetail");
		}
		
		
		if("backToListSubjectDetail".equalsIgnoreCase(eclass.getTask())){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepDetailModel epdm = eclass.getModelExamPrepDetail();
			Integer examPrepId = epdm.getExamPrepId();
			eclass = configListExamPrepDetail(eclass,exPrepMgr,examPrepId);
			return mapping.findForward("addEditExamPrepDetail");
		}
		
		if("goToExamPrepDetail".equalsIgnoreCase(eclass.getTask())){
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			ExamPrepModel epm = eclass.getModelExamPrep();
			//System.out.println(epm);
			Integer examPrepId = epm.getExamPrepId();
			eclass = configListExamPrepDetail(eclass,exPrepMgr,examPrepId);
			return mapping.findForward("addEditExamPrepDetail");
		}
		
		if("getNumOfQuest".equalsIgnoreCase(eclass.getTask())){
			//System.out.println("&**************** getNumOfQuest");
			ExamPrepManager exPrepMgr = new ExamPrepManager();
			Integer numOfQuest = null;
			String append = "";
			
			//for candidate
			//System.out.println("1");
			if(eclass.getModelExamPrep()!=null){
				//System.out.println("2");
				if("candidate".equalsIgnoreCase(eclass.getModelExamPrep().getExamFor())){
					//System.out.println("*************** Masuk getNumOfQuest Candidatee");
					numOfQuest = exPrepMgr.getNumOfQuestForCandidate();
					//System.out.println("3");
					//System.out.println("*************** numOFQuest : "+numOfQuest);
					
					//Rere, 28 Des 2013
					append = "<input type='text' id = 'numberOfQuest' size='3' value='"+numOfQuest.toString()+"' readonly='readonly'/>";
				}
				else{
					//System.out.println("*************** Masuk getNumOfQuest Trainee");
					Integer classId = eclass.getModelExamPrepDetail().classId;
					numOfQuest = exPrepMgr.getListClassFrmQuestPoolById(classId).getNoOfQuestion();
					//System.out.println("*************** NumOfQuest");
					
					//Rere, 28 Des 2013					
					append = numOfQuest.toString();
					//System.out.println("Total Question : " + append);
				}					
			}
			else{
				//System.out.println("*************** getModelExamPrep = NULL!!");
				Integer classId = eclass.getModelExamPrepDetail().classId;
				if(classId != null){
					numOfQuest = exPrepMgr.getListClassFrmQuestPoolById(classId).getNoOfQuestion();
				}
			}
			response.setContentType("text/text;charset=utf-8");
			response.setHeader("cache-control", "no-cache");
			PrintWriter out = response.getWriter();
//			append = "<input type='text' id = 'numberOfQuest' size='3' value='"+numOfQuest.toString()+"' readonly='readonly'/>";
			
			out.println(append);
			out.flush();
			return null;
		}		
		
		//END EXAM PREPARATION
		
		//System.out.println("\n\n\n2 " + eclass.getTask() + "\n\n\n");
		//System.out.println(eclassManager.selectAll());
		eclass.setListUser(eclassManager.selectAll(null));
		//System.out.println(eclassManager.selectAll());
		//eclass.setListUser(eclassManager.selectAll());

		eclass.setUpdateUser("0");
		return mapping.findForward("manageUser");
	}
	
	private Boolean insertExamPrepDetailForCandidate(EclassForm eclass,ExamPrepManager exPrepMgr) {
		Boolean result = false;
		Integer numOfQuest = eclass.getModelExamPrep().getNumOfQuestCandidate();
		System.out.println("Number of Quuestion : " + numOfQuest);
		if(eclass.getModelExamPrep().getExamFor().equalsIgnoreCase("Candidate") && (numOfQuest!=null && numOfQuest>0)){
			Integer examPrepId = eclass.getModelExamPrep().getExamPrepId();
			ExamPrepDetailModel expdm = eclass.getModelExamPrepDetail();
			expdm.setExamPrepId(examPrepId);
			expdm.setClassId(0);
			expdm.setNoOfQuestion(numOfQuest);
			expdm.validateModel();
			
//			if(exPrepMgr.getNumOfExamPrepDetail(examPrepId)	<1){
//				if(exPrepMgr.insertExamPrepDetail(expdm)) 
//					return true;
//			}
//			else{
//				if(exPrepMgr.updateExPrepDetNumOfQuestForCandidate(expdm)) 
//					return true;
//			}
			
			//Rere 31 Des 2013
			if(exPrepMgr.deleteExamPrepDetByExamPrepId(examPrepId)){
				result = exPrepMgr.insertExamPrepDetail(expdm);
			}
		}
		return result;
	}


	//START EXAM PREPARATION Function
	private EclassForm configListExamPrepDetail(EclassForm eclass, ExamPrepManager exPrepMgr,Integer examPrepId){
		//buat list exam prep detail by idExamPrep
		ExamPrepModel mep = eclass.getModelExamPrep();
		System.out.println("configListExamPrepDetail : "+examPrepId);
		//eclass.setListClass(exPrepMgr.getListUniqueClassFrmQuestPool(eclass.getListClass(), mep.getExamFor()));
		eclass.setListExamPrepDetail(exPrepMgr.getExamPrepDetByExamPrepId(examPrepId));
		mep.setExamPrepId(examPrepId);
		eclass.setModelExamPrep(mep);
		System.out.println("configListExamPrepDetail 11");
		eclass.setListClass(getUniqueClassId(eclass, exPrepMgr));
		return eclass;
	}
	
	private EclassForm setAfterInsUpdtExamPrep(EclassForm eclass,ExamPrepManager exPrepMgr){
		//buat list ujian
		ExamPrepModel epm = eclass.getModelExamPrep();
		
		System.out.println("**setAfterInsUpdtExamPrep");
		//System.out.println("2 Question Pool: \n"+eclass.getListClass());
		//buat list exam prep
		eclass.setListExamPrep(exPrepMgr.getRawExamPrepWithSearching(eclass));
		System.out.println("**3\n");
		//System.out.println(eclass.getListExamPrep());
		//buat list exam prep detail by idExamPrep
		Integer examPrepId = epm.getExamPrepId();
		System.out.println("setAfterInsUpdtExamPrep : "+examPrepId);
		System.out.println("4");
		eclass.setListExamPrepDetail(exPrepMgr.getExamPrepDetByExamPrepId(examPrepId));
		
		eclass.setListClass(getUniqueClassId(eclass, exPrepMgr));
		System.out.println("****5\n"+eclass.getListClass());
		
		return eclass;
	}
	
	private EclassForm setListExamPrep(EclassForm eclass){
		ExamPrepManager exPrepMgr = new ExamPrepManager();
		eclass.setListExamPrep(exPrepMgr.getRawExamPrepWithSearching(eclass));
		return eclass;
	}
	
	private EclassForm setNullExamPrep(EclassForm eclass){
		ExamPrepModel epm = eclass.getModelExamPrep();
		if(epm.getBatchClassId()==0) epm.setBatchClassId(null);
		if(epm.getDuration()==0) epm.setDuration(null);
		if("candidate".equalsIgnoreCase(epm.getExamFor())){
			if(epm.getNumOfQuestCandidate() == null) epm.setStatus("0");
			else if(epm.getNumOfQuestCandidate()<1) epm.setStatus("0");
			epm.setBatchClassId(null);
		}
		epm.validateModel();
		
		String startHour = epm.getStartHour();
		if(startHour!=null && !startHour.contains(":")){
			epm.setStartHour(startHour+" 00:00:00");
		}
		
		if(epm.getEndHour()==null || epm.getEndHour().equalsIgnoreCase(""))
			epm.setEndHour(startHour+" 01:00:00");
		//eclass.setTask("Insert");
		eclass.setModelExamPrep(epm);
		System.out.println(epm);
		return eclass;
	}
	
	private List getUniqueClassId(EclassForm eclass, ExamPrepManager exPrepMgr){
		ExamPrepModel epm = eclass.getModelExamPrep();
		return exPrepMgr.getUniqLstClassTraineeFrmQuestPool(epm.getExamPrepId());
	}
	
	private List getUniqueClassIdWithAddition(EclassForm eclass, ExamPrepManager exPrepMgr,int clsIdAdd){
		ExamPrepModel epm = eclass.getModelExamPrep();
		return exPrepMgr.getUniqLstClassTraineeFrmQuestPoolWithAdd(epm.getExamPrepId(),clsIdAdd);
	}
	
	private Boolean cekDuration(Integer duration, String examFor) {
		if("candidate".equalsIgnoreCase(examFor) && duration!=null && duration>0){
			return true;
		}
		return false;
	}
	
	public Boolean cekStartEndDate(String timeStart, String timeEnd,String examFor){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date startTime = null;
		Date endTime = null;
		try {
			startTime = sdf.parse(timeStart);
			endTime = sdf.parse(timeEnd);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}    	
    	
    	/*System.out.println(sdf.format(startTime));
    	System.out.println(sdf.format(endTime));
*/
		System.out.println("startTime - endTime: "+startTime +" - "+endTime);
		if("TRAINEE".equalsIgnoreCase(examFor.toUpperCase()) && startTime.compareTo(endTime)>=0){
    		System.out.println("Date1 is after Date2!!!!!!!!!");
    		return false;
    	}
		return true;
	}
	//END EXAM PREPARATION Function
}
